package com.atguigu.tingshu.search.receiver;

import co.elastic.clients.elasticsearch._types.query_dsl.MatchQuery;
import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.atguigu.tingshu.search.api.SearchApiController;
import com.atguigu.tingshu.search.service.SearchService;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import org.aspectj.lang.annotation.Around;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;



@Component
public class SearchReceiver {
    @Autowired
    private SearchService searchService;

    @Autowired
    private SearchApiController searchApiController;
    @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_ALBUM, durable = "true"),
            value = @Queue(value = MqConst.ROUTING_ALBUM_UPPER,durable = "true"),
            key = {MqConst.ROUTING_ALBUM_UPPER}
    ))
    public void upperGoods(Long albumId, Message message, Channel channel){
        if(albumId!=null){
            searchService.upperAlbum(albumId);
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
    @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_ALBUM, durable = "true"),
            value = @Queue(value = MqConst.ROUTING_ALBUM_LOWER,durable = "true"),
            key = {MqConst.ROUTING_ALBUM_LOWER}
    ))
    public void lowerGoods(Long albumId, Message message, Channel channel){
        if(albumId!=null){
            searchService.lowerAlbum(albumId);
        }
        //消费者端手动应答
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
